package com.millstein.tsinglog.mapper.biz;

import com.millstein.tsinglog.mapper.BaseMapper;
import com.millstein.tsinglog.model.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select("SELECT `id`, `username`, `nickname`, `password`, `salt`, `mobile`, `avatar`, `role_id` roleId, " +
            "`usable`, `last_login_ip` lastLoginIp, `last_login_time` lastLoginTime, `login_count` loginCount, " +
            "`status`, `create_time` createTime, `update_time` updateTime FROM `sys_user` " +
            "where `status` = 1 and `id` = #{id};")
    User selectUserByPrimaryKey(@Param("id") Long id);

    /**
     * 根据权限查询所有拥有此权限的用户名称
     * @param code 权限代码
     * @return
     */
    @Select("SELECT u.nickname FROM `sys_user` u " +
            "LEFT JOIN `sys_role` r on u.role_id = r.id " +
            "LEFT JOIN `sys_role_permission` rp on r.id = rp.role_id " +
            "LEFT JOIN `sys_permission` p on rp.permission_id = p.id " +
            "WHERE u.`status` = 1 and p.`permission_code` = #{code}")
    List<String> selectUserByCode(@Param("code") String code);
}
